<!DOCTYPE html>
<html>
<head>

	<title>Game Summary</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

  <style>

table {
  font-size: 14px;
  }
</style>

</head>
<body>

<div class="container-fluid">
  <div class="row">
    <div class="col-md-2"></div>
    <div class="col-md-8">

<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

if ($_POST) {

require_once  __DIR__ . '/includes/authenticate.php';

require_once __DIR__ . '/includes/BasketbalStatsTableFunctions.php';

require_once __DIR__ . '/includes/BasketbalFunctions.php';

include_once __DIR__ . '/db_bb_connect.php';

$basketbalfunctions = new BasketbalStatsTableFunctions();

$basketbalfunctions_general = new BasketbalFunctions();

$season = $_POST['season'];
$gamenumber = $_POST['gamenumber'];

$username = $_POST['username'];

$user_key = $basketbalfunctions_general->GetUserkeyFromUsername($username);

$sql = "INSERT INTO clicked_items (user_key, clicked_item)
VALUES ('$user_key', 'game_summary_$season"."_$gamenumber')";

		if ($db->query($sql)) {  
    	} else {
			echo "Error: " . $sql . "<br>" . $db->error;
	    }  
	      	

$sql = "SELECT DISTINCT teamname FROM teams  
    WHERE season = '$season'";

$result = $db->query($sql);

$row = $result->fetch_assoc();

$coachteamname = $row['teamname'];


$sql = "SELECT * FROM games  
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'";

$result = $db->query($sql);

$row = $result->fetch_assoc();

$date = $row['date'];

$opponent = $row['opponent'];

$location = $row['location'];

$embed_code = $row['embed_code'];

$nbbstatsfilename = rawurlencode($row['nbbstatsfilename']);

$score_coachteam_total = $row['score_exc_tot'];
$score_coachteam_q1 = $row['score_exc_q1'];
$score_coachteam_q2 = $row['score_exc_q2'];
$score_coachteam_q3 = $row['score_exc_q3'];
$score_coachteam_q4 = $row['score_exc_q4'];
$score_coachteam_ot = $row['score_coach_ot'];

$score_coachteam_after_q2 = $score_coachteam_q1 + $score_coachteam_q2;
$score_coachteam_after_q3 = $score_coachteam_q1 + $score_coachteam_q2 + $score_coachteam_q3;
$score_coachteam_after_q4 = $score_coachteam_q1 + $score_coachteam_q2 + $score_coachteam_q3 + $score_coachteam_q4;

$score_coachteam_after_ot = $score_coachteam_q1 + $score_coachteam_q2 + $score_coachteam_q3 + $score_coachteam_q4 + $score_coachteam_ot;

$score_opponent_total = $row['score_opp_tot'];
$score_opponent_q1 = $row['score_opp_q1'];
$score_opponent_q2 = $row['score_opp_q2'];
$score_opponent_q3 = $row['score_opp_q3'];
$score_opponent_q4 = $row['score_opp_q4'];
$score_opponent_ot = $row['score_opp_ot'];

$score_opponent_after_q2 = $score_opponent_q1 + $score_opponent_q2;
$score_opponent_after_q3 = $score_opponent_q1 + $score_opponent_q2 + $score_opponent_q3;
$score_opponent_after_q4 = $score_opponent_q1 + $score_opponent_q2 + $score_opponent_q3 + $score_opponent_q4;
$score_opponent_after_ot = $score_opponent_q1 + $score_opponent_q2 + $score_opponent_q3 + $score_opponent_q4 + $score_opponent_ot;

$game_comment = nl2br($row['comment']);

echo "<h2>$opponent $location on $date </h2>";

if ( $location == 'away') {


	echo "<h3> Final Result: $score_opponent_total - $score_coachteam_total </h3>";

	?>

  <h4>Quarter-by-Quarter</h4>     
  <table class="table table-striped">
    <thead>
      <tr>
        <th>type</th>
        <th>Q1</th>
        <th>Q2</th>
        <th>Q3</th>
        <th>Q4</th>
            <?php if ($basketbalfunctions->CheckGameStatsOTAvailable($season, $gamenumber)){
					
					echo "<th>OT</th>"; 
				}

				?>

      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Absolute</td>
        <td><?php echo "$score_opponent_q1-$score_coachteam_q1"; ?> </td>
        <td><?php echo "$score_opponent_after_q2-$score_coachteam_after_q2"; ?></td>
        <td><?php echo "$score_opponent_after_q3-$score_coachteam_after_q3"; ?></td>
        <td><?php echo "$score_opponent_after_q4-$score_coachteam_after_q4"; ?></td>

                   <?php if ($basketbalfunctions->CheckGameStatsOTAvailable($season, $gamenumber)){
					
					echo "<td>$score_opponent_after_ot-$score_coachteam_after_ot</td>"; 
				}
?>
      </tr>
      <tr>
        <td>Relative</td>
        <td><?php echo "$score_opponent_q1-$score_coachteam_q1"; ?> </td>
        <td><?php echo "$score_opponent_q2-$score_coachteam_q2"; ?></td>
        <td><?php echo "$score_opponent_q3-$score_coachteam_q3"; ?></td>
        <td><?php echo "$score_opponent_q4-$score_coachteam_q4"; ?></td>

                           <?php if ($basketbalfunctions->CheckGameStatsOTAvailable($season, $gamenumber)){
					
					echo "<td>$score_opponent_ot-$score_coachteam_ot</td>"; 
				}
?>
      </tr>
    </tbody>
  </table>
<?php

} elseif ( $location == 'home')  {

	echo "<h3> Final Result: $score_coachteam_total - $score_opponent_total </h3>";


	?>
<h4>Quarter-by-Quarter</h4>     
  <table class="table table-striped">
    <thead>
      <tr>
        <th>type</th>
        <th>Q1</th>
        <th>Q2</th>
        <th>Q3</th>
        <th>Q4</th>

       <?php if ($basketbalfunctions->CheckGameStatsOTAvailable($season, $gamenumber)){
					
					echo "<th>OT</th>"; 
				}

				?>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Absolute</td>
        <td><?php echo "$score_coachteam_q1-$score_opponent_q1"; ?> </td>
        <td><?php echo "$score_coachteam_after_q2-$score_opponent_after_q2"; ?></td>
        <td><?php echo "$score_coachteam_after_q3-$score_opponent_after_q3"; ?></td>
        <td><?php echo "$score_coachteam_after_q4-$score_opponent_after_q4"; ?></td>

           <?php if ($basketbalfunctions->CheckGameStatsOTAvailable($season, $gamenumber)){
					
					echo "<td>$score_coachteam_after_ot-$score_opponent_after_ot</td>"; 
				}

				?>
      </tr>
      <tr>
        <td>Relative</td>
        <td><?php echo "$score_coachteam_q1-$score_opponent_q1"; ?> </td>
        <td><?php echo "$score_coachteam_q2-$score_opponent_q2"; ?></td>
        <td><?php echo "$score_coachteam_q3-$score_opponent_q3"; ?></td>
        <td><?php echo "$score_coachteam_q4-$score_opponent_q4"; ?></td>

            <?php if ($basketbalfunctions->CheckGameStatsOTAvailable($season, $gamenumber)){
					
					echo "<td>$score_coachteam_ot-$score_opponent_ot</td>"; 
				}

				?>
      </tr>
    </tbody>
  </table>

<?php
	
}
echo "<div> <h3> Stats Totals </h3>";

?>

  <table class="table table-striped">
    <thead>
      <tr>
        <th>Team</th>
              <th>DRB</th>
              <th>ORB</th>
              <th>TO</th>
              <th>ST</th>
          <?php 
          if ($season >= '2018/2019') { 
                echo "<th>AST</th>";
                echo "<th>BS</th>";
              } 

          if ($season < '2017/2018') {
            echo "<th>FG</th>";
          } else if ($season >= '2017/2018') {
            echo "<th>FGI</th>";
            echo "<th>FGO</th>";
            echo "<th>3P</th>";
          }
          ?> 
              <th>FT</th>
              <th>PTS</th>
              <th>PF</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><?= $coachteamname; ?></td>
      <?php
      if ( $basketbalfunctions->CheckGameStatsAllQuartersAvailable($season, $gamenumber, $coachteamname) ) {

              $basketbalfunctions->ShowCumuGameStatsTableColumnsFromQuarters($season, $gamenumber, $coachteamname);

      } elseif ( $basketbalfunctions->CheckGameStatsTotalsAvailable($season, $gamenumber) ) {

        $basketbalfunctions->ShowCumuGameStatsTableColumnsFromTotals($season, $gamenumber);
        
      } else {

          echo "<td colspan='12'> No Stats Available </td>";

        }
      ?>

      </tr>
      <tr>
        <td><?= $opponent; ?></td>
      <?php
      if ( $basketbalfunctions->CheckGameStatsAllQuartersAvailable($season, $gamenumber, $opponent) ) {

              $basketbalfunctions->ShowCumuGameStatsTableColumnsFromQuarters($season, $gamenumber, $opponent);

            } else {

          echo "<td colspan='12'> No Stats Available </td>";

        }

       
        
      ?>
      </tr>
    </tbody>
  </table>

<?php  

 if ($nbbstatsfilename != '') {

          echo "<a href='./pdfs/". $nbbstatsfilename .".pdf' target='_blank'>NBB wedstrijd formulier</a>";
          
        } 



if ( $game_comment != '') {

echo "<div id='gamecomment'><h3>Comment </h3> $game_comment  </div>"; 

}

if ( $embed_code != '') {
?>

<div id="video"><h3> Video </h3>  <center><iframe id="game_video" width="560" height="315" src="https://www.youtube.com/embed/<?= $embed_code?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
 </center> <br></div>"; 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<script type="text/javascript">
	$('#video').on('click', function(event) { 
			//alert("test works");

	});
</script>


<?php

}

} else {
 echo "Incorrect request";
}


?>

  </div>
    <div class="col-md-2"></div>
  </div>
</div>





</body>
</html>
